package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.MedicineStatus;
import com.ruoyi.system.domain.SysClassification;
import com.ruoyi.system.domain.SysMedicines;
import com.ruoyi.system.domain.SysPrescriptions;
import com.ruoyi.system.domain.vo.SysMedicinesVo;
import com.ruoyi.system.domain.vo.SysPrescriptionsVo;
import com.ruoyi.system.mapper.SysMedicinesMapper;
import com.ruoyi.system.service.ISysClassificationService;
import com.ruoyi.system.service.ISysJashuService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SysJashuServiceImpl implements ISysJashuService {

    @Value("${director-id}")
    private Long directorId;

    @Autowired
    private ISysClassificationService classificationService;

    @Autowired
    ISysUserService userService;
    @Autowired
    private SysMedicinesMapper sysMedicinesMapper;

    /**
     * 普通医生只能看到自己提交未审核 和 驳回的药品 或 已审核的所有药品
     * 查询药品信息的查询逻辑
     * @param sysMedicines
     * @return
     */
    @Override
    public SysMedicines getSysMedicinesSelectParam(SysMedicines sysMedicines) {

        String status = sysMedicines.getStatus();
        if(MedicineStatus.Audited.getKey().equals(status)){
            // 如果查看已审核的 就能看到全部
        }else {
            // 如果是未审核  普通医生只能看到自己提交的和驳回的    主任能看到全部
            LoginUser loginUser = SecurityUtils.getLoginUser();
            List<SysRole> roles = loginUser.getUser().getRoles();
            List<Long> list = new ArrayList<>();
            for (SysRole role : roles) {
                list.add(role.getRoleId());
            }
            if(list.contains(directorId) || list.contains(1L)){
                // 主任
            }else {
                // 普通医生查询
                String username = loginUser.getUsername();
                sysMedicines.setCreateBy(username);
            }
        }


        return sysMedicines;
    }

    /**
     * 修改返回参数
     * @param sysMedicines
     * @return
     */
    @Override
    public List<SysMedicinesVo> getSysMedicinesVoList(List<SysMedicines> sysMedicines) {
        List<SysMedicinesVo> vos = new ArrayList<SysMedicinesVo>();
        for (SysMedicines sysMedicine : sysMedicines) {
            Long classificationId = sysMedicine.getClassificationId();
            SysClassification sysClassification = classificationService.selectSysClassificationByClassificationId(classificationId);
            String classificationName = sysClassification.getClassificationName();

            SysMedicinesVo vo = new SysMedicinesVo();
            BeanUtils.copyBeanProp(vo , sysMedicine);
            vo.setClassificationName(classificationName);
            vos.add(vo);
        }

        return vos;
    }


    /**
     * 为查询销售信息表设置查询参数
     * @param sysPrescriptions
     * @return
     */
    @Override
    public SysPrescriptions getSysPrescriptionsSelectParam(SysPrescriptions sysPrescriptions) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        if(!user.isAdmin()){
            List<SysRole> roles = user.getRoles();
            List<Long> list = new ArrayList<>();
            for (SysRole role : roles) {
                list.add(role.getRoleId());
            }
            if(list.contains(directorId)){
                // 主任
                sysPrescriptions.setDoctorId(null);
            }else {
                // 普通医生查询
                String username = loginUser.getUsername();
                sysPrescriptions.setDoctorId(user.getUserId());
            }
        } else {
            // 管理员
            sysPrescriptions.setDoctorId(null);
        }
        return sysPrescriptions;
    }


    /**
     * 设置返回参数
     * @param sysPrescriptions
     * @return
     */
    @Override
    public List<SysPrescriptionsVo> getSysPrescriptionsVoList(List<SysPrescriptions> sysPrescriptions) {
        List<SysPrescriptionsVo> vos = new ArrayList<SysPrescriptionsVo>();
        for (SysPrescriptions sysPrescription : sysPrescriptions) {
            SysPrescriptionsVo vo = new SysPrescriptionsVo();
            Long doctorId = sysPrescription.getDoctorId();
            Long medicineId = sysPrescription.getMedicineId();
            SysMedicines sysMedicines = sysMedicinesMapper.selectSysMedicinesByMedicineId(medicineId);
            vo.setSysMedicines(sysMedicines);
            SysUser sysUser = userService.selectUserById(doctorId);
            BeanUtils.copyBeanProp(vo , sysPrescription);
            vo.setDoctorName(sysUser.getNickName());
            vos.add(vo);
        }
        return vos;
    }
}
